#include "MyListViewItem.h"
#include "MyListView.h"

#include <qpainter.h>

MyListViewItem::MyListViewItem(QListView * parent)
	: QListViewItem(parent)
	, m_bAvailable(false)
	, m_bHavePhone(false)
{
	myListView = (MyListView*)parent;
}

MyListViewItem::~MyListViewItem()
{
//	qDebug("~MyListViewItem");
}

QString fitMessageToWidth(const QString text, const QFontMetrics & fm, int width)
{ 
	if (fm.width(text) <= width)
		return text;
	
	int i = 0;
	QString s = text.section(' ', 0, i);
	if (fm.width(s) > width)
	{
		while (fm.width(s.left(i+1)) < width) i++;
		return s.left(i);	
	}
	else
	{
		while (fm.width(text.section(' ', 0, i + 1)) < width) i++;
		return text.section(' ', 0, i);
	}	
}

void MyListViewItem::paintCell(QPainter * p, const QColorGroup & /* cg */, int /* column */, int width, int /* alignment */)
{
	int x = 0;
	int y = 0;

	int pictureOffset = myListView->m_bShowPicture ? (width - c_nMargin - (height() - 2 * c_nMargin)) : width;

	QFontMetrics fm = p->fontMetrics();

	//foreground
	p->fillRect(0, 0, width, height(), isSelected() ? QColor(214, 217, 229) : Qt::white);

	//status icon
	QPixmap & pixmapStatus = m_bAvailable ? myListView->pixmapStatusAvailable : myListView->pixmapStatusUnavailable;

	p->drawPixmap(c_nMargin + ((c_nSps + fm.height() + c_nSps - pixmapStatus.width()) >> 1),
		c_nMargin + ((fm.height() - pixmapStatus.height()) >> 1),
		pixmapStatus);


	if (myListView->m_bShowPicture)
	{
		//phone
		QPixmap & pixmapAvatar = myListView->pixmapAvatar;

		p->drawPixmap(pictureOffset + ((height() - 2 * c_nMargin - pixmapAvatar.width()) >> 1),
			c_nMargin + ((height() - 2 * c_nMargin - pixmapAvatar.height()) >> 1),
			pixmapAvatar);
	}

	if (m_bHavePhone)
	{
		//phone
		QPixmap & pixmapPhone = isSelected() ? (myListView->hoverPhone ? myListView->pixmapPhoneHover : myListView->pixmapPhoneSelected) : myListView->pixmapPhoneNormal;

		p->drawPixmap(pictureOffset - c_nMargin - (c_nSps + fm.height() + c_nSps) + ((c_nSps + fm.height() + c_nSps - pixmapPhone.width()) >> 1),
			c_nMargin + ((fm.height() - pixmapPhone.height()) >> 1),
			pixmapPhone);
	}

	x = c_nMargin + (c_nSps + fm.height() + c_nSps) + c_nSpacing;
	y = c_nMargin + fm.ascent();

	int delta = pictureOffset - 2 * (c_nMargin + (c_nSps + fm.height() + c_nSps) + c_nSpacing);

	//screenname
	p->drawText(x, y, fitMessageToWidth(text(0), fm, delta));


	if (!m_sStatus.isEmpty() || myListView->m_bShowPicture)
	{
		QString sStatus = fitMessageToWidth(m_sStatus.isEmpty() ? (m_bAvailable ? QObject::tr("Available") : QObject::tr("Offline")) : m_sStatus, fm, delta);
	
		p->setPen(isSelected() ? QColor(92, 92, 92) : Qt::gray);
		y += c_nSpacing + fm.height();
		p->drawText(x, y, sStatus);
	}

	if (!isSelected())
	{
		QListViewItem * belowItem = itemBelow();
		if (!belowItem || !belowItem->isSelected())
		{
			//horizontal line at bottom
			p->setPen(QColor(240, 240, 240));
			p->drawLine(c_nMargin, height() - 1, width - c_nMargin, height() - 1);
		}
	}
}


void MyListViewItem::paintFocus(QPainter * /* p */, const QColorGroup & /* cg */, const QRect & /* r */)
{
//	QListViewItem::paintFocus(p, cg, r);
}

void MyListViewItem::paintBranches(QPainter * /* p */, const QColorGroup & /* cg */, int /* i0 */, int /* i1 */, int /* i2 */)
{
//	QListViewItem::paintBranches(p, cg, i0, i1, i2);
}

void MyListViewItem::setup()
{
	QFontMetrics fm = myListView->fontMetrics();

	QListViewItem::setup();

	if (!m_sStatus.isEmpty() || myListView->m_bShowPicture)
		setHeight(c_nMargin + fm.height() + c_nSpacing + fm.height() + c_nMargin);
	else
		setHeight(c_nMargin + fm.height() + c_nSpacing + c_nMargin);
}

void MyListViewItem::setHeight(int height)
{
	QListViewItem::setHeight(height);
}

void MyListViewItem::setSelected(bool s)
{
	QListViewItem::setSelected(s);

	QListViewItem * above = itemAbove();
	if (above) above->repaint();
}

int MyListViewItem::compare(QListViewItem * i, int col, bool ascending) const
{
	if (myListView->m_bSortByName)
		return QListViewItem::compare(i, col, ascending);

	MyListViewItem * item = (MyListViewItem *)i;
	if (item->m_bAvailable == m_bAvailable)
		return QListViewItem::compare(item, col, ascending);

	return m_bAvailable ? -1 : 1;
}
